import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zootix/apis/zt-api.dart';
import 'package:zootix/constants/zt_constants.dart';
import 'package:zootix/models/zt_followed.model.dart';
import 'package:dio/dio.dart' as Dio;
import 'package:zootix/screens/rank/zt_rank.controller.dart';
import 'package:zootix/services/zt_service.dart';
import 'package:zootix/store/zt_im_chat.store.dart';
import 'package:zootix/util/zt_convert.dart';
import 'package:zootix/util/zt_loading_util.dart';
import 'package:zootix/util/zt_snack_bar.util.dart';

class ZtFollowedController extends GetxController {
  final followedList = <ZtFollowedModel>[].obs;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  int page = 1;
  int limit = 30;
  int total = 0;
  @override
  onReady() {
    super.onReady();
  }

  Future<void> getData({bool isRefresh = false}) async {
    if (isRefresh == true) {
      page = 1;
    } else {
      page++;
    }
    Dio.Response response = await getFollowedList(page, limit);
    if (response.data != null && response.data['data'] != null) {
      final newListData = toList(response.data['data'])
          .map((e) => ZtFollowedModel.fromJson(e))
          .toList();
      total = followedList.length < limit ? followedList.length : limit + 1;
      isRefresh == true
          ? followedList.assignAll(newListData)
          : followedList.addAll(newListData);
    }
  }

  Future<void> onRefresh() async {
    await getData(isRefresh: true).then((_) {
      refreshController.refreshCompleted(resetFooterState: true);
    }).catchError((_) {
      refreshController.refreshFailed();
    });
  }

  Future<void> onLoading() async {
    if (followedList.length < total) {
      await getData().then((_) {
        refreshController.loadComplete();
      }).catchError((_) {
        refreshController.loadFailed();
      });
    } else {
      refreshController.loadNoData();
    }
  }

  Future<void> unFollowed(String userId, int? index) async {
    showCupertinoDialog(
        context: Get.context!,
        builder: (_) => CupertinoAlertDialog(
              title: const Text('Are you sure you want to unfollow?'),
              actions: <Widget>[
                CupertinoDialogAction(
                  child: const Text('Cancel'),
                  onPressed: () {
                    Get.back(closeOverlays: true);
                  },
                ),
                CupertinoDialogAction(
                  child: const Text('Confirm'),
                  onPressed: () async {
                    Get.back(closeOverlays: true);
                    ZtLoadingUtil.show(status: 'Unfollowing...');
                    try {
                      final response = await removeFriend(userId);
                      if (response.data != null) {
                        if (index != null) {
                          followedList.removeAt(index);
                          ZtRankController()
                              .asyncRankListFollowed(userId, false);
                        }
                      }
                    } catch (e) {
                    } finally {
                      EasyLoading.dismiss();
                    }
                  },
                ),
              ],
            ));
  }
}
